package cn.iluwen.airline.Mapper;

import cn.iluwen.airline.Entities.Seat;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SeatMapper {

    /**
     * 查询所有座位
     * @return 座位列表
     */
    @Select("SELECT * FROM seats")
    List<Seat> findAllSeats();
    
    /**
     * 分页查询座位
     * @return 座位列表
     */
    @Select("SELECT * FROM seats ORDER BY seat_id")
    List<Seat> findSeatsWithPaging();
    
    /**
     * 根据航班ID查询座位
     * @param flightId 航班ID
     * @return 座位列表
     */
    @Select("SELECT * FROM seats WHERE flight_id = #{flightId}")
    List<Seat> findByFlightId(String flightId);
    
    /**
     * 根据航班ID和舱位等级查询座位
     * @param flightId 航班ID
     * @param seatClass 舱位等级（economy/business）
     * @return 座位列表
     */
    @Select("SELECT * FROM seats WHERE flight_id = #{flightId} AND seat_class = #{seatClass}")
    List<Seat> findByFlightIdAndClass(@Param("flightId") String flightId, @Param("seatClass") String seatClass);
    
    /**
     * 根据航班ID和座位号查询座位
     * @param flightId 航班ID
     * @param seatNumber 座位号
     * @return 座位对象
     */
    @Select("SELECT * FROM seats WHERE flight_id = #{flightId} AND seat_number = #{seatNumber}")
    Seat findByFlightIdAndSeatNumber(@Param("flightId") String flightId, @Param("seatNumber") String seatNumber);
    
    /**
     * 查询航班的可用座位
     * @param flightId 航班ID
     * @return 可用座位列表
     */
    @Select("SELECT * FROM seats WHERE flight_id = #{flightId} AND is_occupied = false")
    List<Seat> findAvailableSeatsByFlightId(String flightId);
    
    /**
     * 根据ID查询座位
     * @param seatId 座位ID
     * @return 座位对象
     */
    @Select("SELECT * FROM seats WHERE seat_id = #{seatId}")
    Seat findById(String seatId);
    
    /**
     * 添加座位
     * @param seat 座位对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO seats (seat_id, flight_id, seat_number, seat_class, is_occupied) " +
            "VALUES (#{seatId}, #{flightId}, #{seatNumber}, #{seatClass}, #{isOccupied})")
    int insert(Seat seat);
    
    /**
     * 批量添加座位
     * @param seats 座位列表
     * @return 影响的行数
     */
    @Insert("<script>" +
            "INSERT INTO seats (seat_id, flight_id, seat_number, seat_class, is_occupied) VALUES " +
            "<foreach collection='list' item='seat' separator=','>" +
            "(#{seat.seatId}, #{seat.flightId}, #{seat.seatNumber}, #{seat.seatClass}, #{seat.isOccupied})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<Seat> seats);
    
    /**
     * 更新座位信息
     * @param seat 座位对象
     * @return 影响的行数
     */
    @Update("UPDATE seats SET flight_id = #{flightId}, seat_number = #{seatNumber}, " +
            "seat_class = #{seatClass}, is_occupied = #{isOccupied} WHERE seat_id = #{seatId}")
    int update(Seat seat);
    
    /**
     * 更新座位占用状态
     * @param seatId 座位ID
     * @param isOccupied 是否被占用
     * @return 影响的行数
     */
    @Update("UPDATE seats SET is_occupied = #{isOccupied} WHERE seat_id = #{seatId}")
    int updateOccupiedStatus(@Param("seatId") String seatId, @Param("isOccupied") Boolean isOccupied);
    
    /**
     * 删除座位
     * @param seatId 座位ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM seats WHERE seat_id = #{seatId}")
    int delete(String seatId);
    
    /**
     * 根据航班ID删除所有座位
     * @param flightId 航班ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM seats WHERE flight_id = #{flightId}")
    int deleteByFlightId(String flightId);
} 